import {queryNearbyShopList} from "@src/api/booking";
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from "@src/store/base";
import location from "@src/store/location";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor'],
  }, {
    store: location,
    fields: ['latitude', 'longitude'],
    actions: ['getLocation']
  }],
  data: {
    isInputFocus: false,
    inputValue: '',
  },
  methods: {
    onLoad() {
      wx.nextTick(()=> {
        this.queryNearbyShopListReq();
      })
    },
    inputFocus() {
      this.setData({isInputFocus: true})
    },
    inputBlur() {
      this.setData({isInputFocus: false})
      this.queryNearbyShopListReq();
    },
    bindKeyInput: function (e) {
      this.setData({
        inputValue: e.detail.value
      })
    },
    chooseItem(e) {
      let shopId = e.currentTarget.dataset.shopid
      const eventChannel = this.getOpenerEventChannel()
      if (eventChannel) {
        eventChannel.emit('changeShop', shopId);
      }
      wx.navigateBack()
    },
    clearInputValue() {
      this.setData({inputValue: ""})
    },
    queryNearbyShopListReq() {
      let data = {
        shopName: this.data.inputValue,
        latitude: this.data.latitude,
        longitude: this.data.longitude
      };
      return queryNearbyShopList(data).then(res => {
        if (res.code === 0) {
          this.setData({nearbyShopList: res.data})
        }
      })
    }
  }
})
